package com.archerdu.apply.controller.admin;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.Advertise;
import com.archerdu.apply.model.AdvertiseCatalog;
import com.archerdu.apply.model.User;
import com.archerdu.apply.service.AdvertiseCatalogManager;
import com.archerdu.apply.service.AdvertiseManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.base.dao.IPage;
import com.archerdu.commons.constant.DataConstant;
import com.archerdu.commons.constant.ErrorConstant;

@Controller
@Scope("prototype")
@RequestMapping("/admin/advertise")
public class AdvertiseController
        extends BaseController {
    
    private static final long    serialVersionUID = 3298072203679257178L;
    
    private AdvertiseCatalogManager advertiseCatalogManager;
    private AdvertiseCatalog        advertiseCatalog;
    private AdvertiseManager     advertiseManager;
    private Advertise            advertise;
    
    @RequestMapping("/main")
    public String main() {
        return "admin/advertise/main";
    }
    
    @RequestMapping(value = "/add")
    public ModelAndView addI(HttpServletRequest requ, AdvertiseController mc) {
        ModelAndView mv = new ModelAndView();
        // 没有目录不能添加文章
        if (mc.getAdvertise() == null || null == mc.getAdvertise().getCatalogId()
                || null == advertiseCatalogManager.get(mc.getAdvertise().getCatalogId())) {
            this.addMessage(ErrorConstant.ARTICLE_CATE_NOT_NULL);
            mv.setViewName("common/error");
        }
        else {
            advertise = mc.getAdvertise();
            
            mv.setViewName("admin/advertise/add");
            
            if ("get".equalsIgnoreCase(requ.getMethod())) {
                mv.addObject(advertise);
            }
            else {
                try {
                    advertise.setCreateUser(((User) requ.getSession().getAttribute(DataConstant.SESSION_SYS_USER))
                            .getId());
                    advertise.setCreatetime(getSqlDate());
                    advertiseManager.add(advertise);
                    addMessage(DataConstant.COMMON_ADD_SUCCESS);
                }
                catch (Exception e) {
                    addMessage(DataConstant.COMMON_ADD_FAILED);
                    logger.error(e.getMessage(), e);
                }
            }
        }
        mv.addObject(getMessage());
        return mv;
    }
    
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest requ, AdvertiseController mc) {
        
        logger.debug("advertise.id is " + mc.getAdvertise().getId());
        advertise = mc.getAdvertise();
        
        if ("get".equalsIgnoreCase(requ.getMethod())) {
            if (!StringUtils.isEmpty(advertise.getId())) {
                advertise = advertiseManager.get(advertise.getId());
            }
        }
        else {
            advertise.setUpdateUser(((User) requ.getSession().getAttribute((DataConstant.SESSION_SYS_USER))).getId());
            advertise.setUpdatetime(getSqlDate());
            try {
                advertiseManager.modify(advertise);
                advertise = advertiseManager.get(advertise.getId());
                addMessage(DataConstant.COMMON_EDIT_SUCCESS);
            }
            catch (Exception e) {
                addMessage(DataConstant.COMMON_EDIT_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        
        ModelAndView mav = new ModelAndView();
        mav.addObject(getMessage());
        mav.addObject(advertise).setViewName("admin/advertise/edit");
        return mav;
    }
    
    @RequestMapping(value = "/remove")
    public ModelAndView remove(HttpServletRequest requ, AdvertiseController mc, Integer pageNo, Integer pageSize) {
        advertise = mc.getAdvertise();
        if (null != advertise && null != advertise.getId()) {
            advertise = advertiseManager.get(advertise.getId());
            try {
                advertiseManager.modify(advertise);
                addMessage(DataConstant.COMMON_DEL_SUCCESS);
            }
            catch (Exception e) {
                addMessage(DataConstant.COMMON_DEL_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        ModelAndView mav = page(requ, mc, pageNo, pageSize);
        mav.addObject(getMessage());
        return mav;
    }
    
    /**
     * 分页查询
     * 
     * @param requ
     * @param ac
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/page")
    public ModelAndView page(HttpServletRequest requ, AdvertiseController ac, Integer pageNo, Integer pageSize) {
        
        ModelAndView mav = new ModelAndView();
        if (null == ac.getAdvertise() || null == ac.getAdvertise().getCatalogId()) {
            addMessage(ErrorConstant.ADVERTISE_SEARCH_NOT_POSITION_CATE);
        }
        
        advertise = ac.getAdvertise();
        // 检查目录类型
        advertiseCatalog = advertiseCatalogManager.get(advertise.getCatalogId());
        if (!advertiseCatalog.getType().equals(DataConstant.ADVERTIES_CATE_TYPE_POSITION)) {
            addMessage(ErrorConstant.ADVERTISE_SEARCH_NOT_POSITION_CATE);
        }
        
        if (null == this.getMessage().getInfo()) {
            
            if (null == pageNo || null == pageSize) {
                pageNo = 1;
                pageSize = 20;
            }
            logger.debug("advertiseManager is " + advertiseManager);
            IPage<Advertise> page = advertiseManager.pageSearch(advertise, pageNo, pageSize);
            mav.addObject(advertise);
            mav.addObject(page).setViewName("admin/advertise/page");
        }else{
            mav.addObject("message", getMessage());
            mav.setViewName("common/info");
        }
        return mav;
    }
    
    public AdvertiseCatalogManager getAdvertiseCatalogManager() {
        return advertiseCatalogManager;
    }
    
    public void setAdvertiseCatalogManager(AdvertiseCatalogManager advertiseCatalogManager) {
        this.advertiseCatalogManager = advertiseCatalogManager;
    }
    
    public AdvertiseCatalog getAdvertiseCatalog() {
        return advertiseCatalog;
    }
    
    public void setAdvertiseCatalog(AdvertiseCatalog advertiseCatalog) {
        this.advertiseCatalog = advertiseCatalog;
    }
    
    public AdvertiseManager getAdvertiseManager() {
        return advertiseManager;
    }
    
    public void setAdvertiseManager(AdvertiseManager advertiseManager) {
        this.advertiseManager = advertiseManager;
    }
    
    public Advertise getAdvertise() {
        return advertise;
    }
    
    public void setAdvertise(Advertise advertise) {
        this.advertise = advertise;
    }
}
